/**
 * 双指针
 * @param {number[]} nums
 * @return {number[]}
 */
export var sortedSquares = function (nums) {
  let left = 0
  let right = nums.length - 1
  const res = []
  while (left <= right) {
    if (Math.abs(nums[left]) > Math.abs(nums[right])) {
      res.unshift(nums[left] * nums[left])
      left++
    } else {
      res.unshift(nums[right] * nums[right])
      right--
    }
  }
  return res
}

/**
 * 重排序
 * @param {number[]} nums
 * @return {number[]}
 */
export const sortedSquares2 = function (nums) {
  return nums.map(i => i ** 2).sort((a, b) => a - b)
}

/**
 * @param {number[]} nums
 * @return {number[]}
 */
export const sortedSquares3 = function (nums) {
  let left = 0
  let right = nums.length - 1
  const ans = new Array(nums.lenght).fill(0)
  let cur = right
  while (left <= right) {
    if (Math.abs(nums[left]) > Math.abs(nums[right])) {
      ans[cur] = nums[left] ** 2
      left++
    } else {
      ans[cur] = nums[right] ** 2
      right--
    }
    cur--
  }
  return ans
}
